package com.n5x.common.upload;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;



/**
 * 上传文件工具类
 * @author libo
 */
public class FileUpload {

    /**
     * 上传文件
     * @param request
     * @param response
     * @param path             文件路径（不包含服务器地址：upload/）
     * @return
     */
    public static String upload(MultipartFile file, HttpServletRequest request,HttpServletResponse response, String path){
        // 文件名称生成策略（UUID uuid = UUID.randomUUID()）.
        try {
            Date d = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
            String formatDate = format.format(d);
            String str = "";
            for(int i=0 ;i <5; i++){
                int n = (int)(Math.random()*90)+10;
                str += n;
            }
            // 获取文件的扩展名
            String extension = FilenameUtils.getExtension(file.getOriginalFilename());
            // 文件名
            String fileName = formatDate + str + "." + extension;
            //相对路径
            String relaPath = path + fileName;

//            String a = serverPath + path.substring(0, path.lastIndexOf("/"));
            // 项目在容器中实际发布运行的根路径
            String realPath=request.getSession().getServletContext().getRealPath("")+relaPath;

            if(realPath.contains("\\n5xproject")||realPath.contains("/n5xproject")){
                realPath = realPath.replace("\\n5xproject","");
                realPath = realPath.replace("/n5xproject","");
            }
            File file2 = new File(realPath);
            if(!file2.exists()){
                boolean mkdirs = file2.mkdirs();
                System.out.println(mkdirs);
            }
            file.transferTo(file2);
            // 另一台tomcat的URL（真实路径）
    //        String realPath = serverPath + relaPath;
            // 设置请求路径
    //        WebResource resource = client.resource(realPath);

            // 发送开始post get put（基于put提交）

    //            resource.post(String.class, file.getBytes());
                return fileName+";"+relaPath+";"+realPath;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 删除文件
     * @param filePath（文件完整地址：http://172.16.5.102:8090/upload/1234.jpg）
     * @return
     */
/*    public static String delete(String filePath){
        try {
            Client client = new Client();
            WebResource resource = client.resource(filePath);
            resource.delete();
            return "y";
        } catch (Exception e) {
            e.printStackTrace();
            return "n";
        }
    }*/
}